﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.TranslateApprove.Model;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using DBUtility;
using System.Data;

public partial class Police_TranslateApprove_ApplyView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        Bind();
        //}
    }
    private void Bind()
    {
        if (Request["FID"] == null)
            return;

        ApplyInfo info = Yipusoft.TranslateApprove.BLL.Apply.GetApply(new Guid(Request["FID"]));
        if (info == null)
            return;

        lblTitle.Text = info.Title;
        lblName.Text = Yipusoft.Account.BLL.User.GetUser(info.UserID).UserName;
        LinkFile.NavigateUrl = "WordFile/" + info.FilePath;

        List<SetupLogInfo> sinfo = Yipusoft.TranslateApprove.BLL.SetupLog.GetList("FID='" + info.AID + "'");
        if (sinfo != null && sinfo.Count > 0)
        {
            HID.Value = sinfo[0].SID.ToString();
            BindHistory(info.AID);
            BindTable(sinfo[0].KID, sinfo[0].PID);
        }
        SQLHelper db = new SQLHelper();

        DataTable dt = db.GetDataTableBySqlStr("select SoldierName from Translate as a,TranslateLogs as b, Apply as c where a.ID=b.FID and b.DID=c.AID and c.AID='" + Request["FID"].ToString() + "'");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (i > 0)
                lblSoldierName.Text += "&nbsp;&nbsp;";
            lblSoldierName.Text += dt.Rows[i][0].ToString().Trim();
        }

        //TranslateLogInfo logInfo = TranslateLogs.GetDetailByDID(info.AID);
        //TranslateInfo Tinfo = Translate.GetDetail(logInfo.FID);
        //lblSoldierName.Text = Tinfo.SoldierName;

    }

    private void BindHistory(Guid GID)
    {
        List<SetupLogInfo> list = Yipusoft.TranslateApprove.BLL.SetupLog.GetList("FID='" + GID.ToString() + "' And State=2");
        lblHistory.Text = "";
        foreach (SetupLogInfo s in list)
        {
            lblHistory.Text += s.Remark + "&nbsp;&nbsp;审批人：" + s.UserName + "&nbsp;&nbsp;审批时间：" + s.RTime.ToString("yyyy-MM-dd hh:mm:ss") + "<br/>";
        }

    }
    protected void BtnOK_Click(object sender, EventArgs e)
    {
        if (HID.Value.Trim() == "")
            return;

        lblPromt.Text = "";

        SetupLogInfo info = Yipusoft.TranslateApprove.BLL.SetupLog.GetModel(new Guid(HID.Value));
        MemberInfo Minfo = Yipusoft.TranslateApprove.BLL.Members.GetMember(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
        Yipusoft.TranslateApprove.Model.ProcessInfo Pinfo = Yipusoft.TranslateApprove.BLL.Process.GetModelByRID(Minfo.DID);
        if (Pinfo == null || Minfo == null || info == null)
            return;

        int Flag = 0;

        info.PID = Yipusoft.TranslateApprove.BLL.Process.GetModelByRID(Minfo.DID).PID;
        info.Remark = txtIntro.Text.Trim();
        info.State = 2;
        info.UserID = User.Identity.Name;
        info.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
        if (Yipusoft.TranslateApprove.BLL.SetupLog.Add(info) > 0)
        {
            lblPromt.Text = "请示已通过";
        }
        else
        {
            lblPromt.Text = "操作失败";
        }

        for (int i = 0; i < TabList.Rows[0].Cells.Count; i++)
        {
            if (TabList.Rows[0].Cells[i].Attributes["PID"] != null)
            {
                if (int.Parse(TabList.Rows[0].Cells[i].Attributes["PID"].ToString()) > (info.PID + 1))
                {
                    if (TabList.Rows[0].Cells[i].HasControls())
                    {
                        if (TabList.Rows[0].Cells[i].Controls[0] is RadioButton)
                        {
                            RadioButton Btn = TabList.Rows[0].Cells[i].Controls[0] as RadioButton;
                            if (!Btn.Checked)
                            {
                                if ((int.Parse(TabList.Rows[0].Cells[i].Attributes["PID"].ToString()) - Pinfo.PID) > 1)
                                {
                                    info.PID = Yipusoft.TranslateApprove.BLL.Process.GetModelByRID(Minfo.DID).PID;
                                    info.Remark = txtIntro.Text.Trim();
                                    info.State = 4;
                                    info.UserID = User.Identity.Name;
                                    info.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                                    Yipusoft.TranslateApprove.BLL.SetupLog.Add(info);
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

            }
        }


        this.BtnOK.Enabled = false;
        this.BtnNo.Enabled = false;


    }

    protected void BtnNo_Click(object sender, EventArgs e)
    {
        if (HID.Value.Trim() == "")
            return;
        lblPromt.Text = "";
        SetupLogInfo info = Yipusoft.TranslateApprove.BLL.SetupLog.GetModel(new Guid(HID.Value));
        MemberInfo Minfo = Yipusoft.TranslateApprove.BLL.Members.GetMember(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
        Yipusoft.TranslateApprove.Model.ProcessInfo Pinfo = Yipusoft.TranslateApprove.BLL.Process.GetModelByRID(Minfo.DID);
        if (Pinfo == null || Minfo == null || info == null)
            return;

        info.PID = Yipusoft.TranslateApprove.BLL.Process.GetModelByRID(Minfo.DID).PID;
        info.Remark = txtIntro.Text.Trim();
        info.State = 3;

        if (Yipusoft.TranslateApprove.BLL.SetupLog.Add(info) > 0)
        {
            lblPromt.Text = "请示已驳回";
        }
        else
        {
            lblPromt.Text = "操作失败";
        }
        this.BtnOK.Enabled = false;
        this.BtnNo.Enabled = false;
    }

    private void BindTable(int KID, int pid)
    {
        TableRow drw = new TableRow();
        TableCell cel = null;
        int PID = pid;
        LiteralControl lbltext = null;
        bool bl = true;
        List<Yipusoft.TranslateApprove.Model.ProcessInfo> ListProcess = Yipusoft.TranslateApprove.BLL.Process.GetList("KID=" + KID + "");

        if (ListProcess != null)
        {
            foreach (Yipusoft.TranslateApprove.Model.ProcessInfo p in ListProcess)
            {

                cel = new TableCell();
                if (p.IsJump && p.PID > (PID + 1))
                {
                    RadioButton RBtn = new RadioButton();
                    RBtn.Text = "<font color=green>提交该级</font>";
                    RBtn.GroupName = "Skip";
                    if (bl)
                    {
                        RBtn.Checked = true;
                        bl = false;
                    }
                    cel.Controls.Add(RBtn);

                }

                cel.Attributes["PID"] = p.OrderID.ToString();
                cel.CssClass = "tabstyle";
                lbltext = new LiteralControl();

                if ((PID + 1) == p.PID)
                {
                    lbltext.Text += "<font color=red>-->" + Yipusoft.TranslateApprove.BLL.JoinType.GetJoinType(p.JID).TypeName + "[" + Yipusoft.TranslateApprove.BLL.Roles.GetRoleInfo(p.RID).DutyName + "]" + "</font>";
                }
                else
                {
                    lbltext.Text += "<font color=red>--></font>" + Yipusoft.TranslateApprove.BLL.JoinType.GetJoinType(p.JID).TypeName + "[" + Yipusoft.TranslateApprove.BLL.Roles.GetRoleInfo(p.RID).DutyName + "]";
                }

                cel.Controls.Add(lbltext);
                drw.Cells.Add(cel);

            }

        }
        TabList.Rows.Add(drw);
    }




}
